package semspe;
import javax.swing.*;
import java.awt.event.*;

class ModifTypeAlcool extends JFrame implements ActionListener//implements WindowListener
{
	private JPanel panel;
	private JLabel Libellé;
	private JLabel Libellé2;
	
	
	private JComboBox txtLibellé;
        private JTextField txtLibellé2;
	

	
	private JOptionPane jop1;
	private JButton OK;
	private JButton annuler;
	
	public ModifTypeAlcool()
	{
		super("Modifier un type d'alcool");
		this.setSize(400,250);
		this.setResizable(false);
		this.setVisible(true);
		
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.panel = new JPanel();
		//Numero type alcool

		
		//Nom type alcool
		//Stock
		this.Libellé = new JLabel("Libellé actuel :");
		this.getContentPane().add(this.Libellé);
		this.Libellé.setBounds(30,20,150,20);
		this.txtLibellé = new JComboBox();
		this.getContentPane().add(this.txtLibellé);
		this.txtLibellé.setBounds(200,20,150,20);
                TraitRequete ligneAlcool = new TraitRequete();
                String [] resultatVouluLigneAlcool = new String[]{"Count(libelle)"};
                String [][] resRecupLigneAlcool = ligneAlcool.ExecuteRequete("SELECT Count(libelle) FROM typealcool", resultatVouluLigneAlcool,1 );
                TraitRequete recupAlcool = new TraitRequete();
                String [] resultatVouluAlcool = new String[]{"libelle"};
                String [][] resRecupAlcool = recupAlcool.ExecuteRequete("SELECT libelle FROM typealcool;", resultatVouluAlcool, Integer.parseInt(resRecupLigneAlcool[0][0]));
                int i=0;
                for(i=0; i<resRecupAlcool[0].length;i++)
                {
                  txtLibellé.addItem(resRecupAlcool[0][i]);  
                }
		
                
                //Nouveau libellé
                this.Libellé2 = new JLabel("Nouveau libellé :");
		this.getContentPane().add(this.Libellé2);
		this.Libellé2.setBounds(30,60,150,20);
                this.txtLibellé2 = new JTextField("");
		this.getContentPane().add(this.txtLibellé2);
		this.txtLibellé2.setBounds(200,60,150,20);
		
		
		//BUT
		this.OK = new JButton("Ajouter");
		this.getContentPane().add(this.OK);
		this.OK.setBounds(100,150,100,20);
		this.annuler = new JButton("Annuler");
		this.getContentPane().add(this.annuler);
		this.annuler.setBounds(250,150,100,20);
		
		this.annuler.addActionListener(this);
		this.OK.addActionListener(this);
		
		jop1 = new JOptionPane();
		
		
	}
	

	public void actionPerformed(ActionEvent evt) {
			if(evt.getSource() == annuler)
			this.dispose();
			else if(evt.getSource() == OK){
                            if(testType()==true){
                        String requete = "UPDATE typealcool SET libelle ='"+txtLibellé2.getText()+"' WHERE libelle ='"+txtLibellé.getSelectedItem()+"'";
                        TraitRequete ajoutType = new TraitRequete();
                            ajoutType.ExecuteRequeteData(requete);
                            this.dispose();
                            }
                        }
	}                

        public boolean testType(){
            
                if(this.txtLibellé.getSelectedItem().equals("")==false){
                    
                                      
                    return true;
                }
            
            jop1.showMessageDialog(null, "Des champs sont vides");
            return false;
        }
        
        
        public boolean testInt(String chaine) {
        try {
        int i = Integer.parseInt(chaine);
        }
        catch (Exception e) {
        return false;
        }
        return true;
        }
        
	
	public static void main (String[] a)
	{
		ModifTypeAlcool modifType1 = new ModifTypeAlcool();
	}
	
} 